package com.zj.sy.api;

import lombok.Data;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * @description: API统一返回结果封装
 * @author: Carver Zhang
 * @create: 2020-11-02 14:45
 **/
@Data
public class ApiResult {

    private int code;
    private String msg;
    private Object data;
    private boolean success;

    private ApiResult(ApiResultCode resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private ApiResult(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = ApiResultCode.SUCCESS.code == code;
    }

    private ApiResult(ApiResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }


    private ApiResult(ApiResultCode resultCode, Object data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    /**
     * 判断返回是否为成功
     *
     * @param result Result
     * @return 是否成功
     */
    public static boolean isSuccess(ApiResult result) {
        return Optional.ofNullable(result)
                .map(x -> ObjectUtils.nullSafeEquals(ApiResultCode.SUCCESS.code, x.code))
                .orElse(Boolean.FALSE);
    }

    /**
     * 判断返回是否为成功
     *
     * @param result Result
     * @return 是否成功
     */
    public static boolean isNotSuccess(ApiResult result) {
        return !ApiResult.isSuccess(result);
    }

    /**
     * 返回R
     *
     * @param data 数据
     * @return R
     */
    public static ApiResult data(Object data) {
        return data(data, PalSystemConstant.DEFAULT_SUCCESS_MESSAGE);
    }

    /**
     * 返回R
     *
     * @param data 数据
     * @param msg  消息
     * @return R
     */
    public static ApiResult data(Object data, String msg) {
        return data(HttpServletResponse.SC_OK, data, msg);
    }

    /**
     * 返回R
     *
     * @param code 状态码
     * @param data 数据
     * @param msg  消息
     * @return R
     */
    public static ApiResult data(int code, Object data, String msg) {
        return new ApiResult(code, data, data == null ? PalSystemConstant.DEFAULT_NULL_MESSAGE : msg);
    }

    /**
     * 返回R
     *
     * @param msg 消息
     * @return R
     */
    public static ApiResult success(String msg) {
        return new ApiResult(ApiResultCode.SUCCESS, msg);
    }

    /**
     * 返回R
     *
     * @param resultCode 业务代码
     * @return R
     */
    public static ApiResult success(ApiResultCode resultCode) {
        return new ApiResult(resultCode);
    }

    /**
     * 返回R
     *
     * @param resultCode 业务代码
     * @param msg        消息
     * @return R
     */
    public static ApiResult success(ApiResultCode resultCode, String msg) {
        return new ApiResult(resultCode, msg);
    }

    /**
     * 返回R
     *
     * @param msg 消息
     * @return R
     */
    public static ApiResult fail(String msg) {
        return new ApiResult(ApiResultCode.FAILURE, msg);
    }

    /**
     * @Description: 请求未授权
     * @Param:
     * @Return: com.zj.sy.api.ApiResult
     * @Author: LJJ
     * @Date: 2022/7/11
     */
    public static ApiResult unauthorized() {
        return new ApiResult(ApiResultCode.UN_AUTHORIZED, ApiResultCode.UN_AUTHORIZED.message);
    }


    /**
     * 返回R
     *
     * @param code 状态码
     * @param msg  消息
     * @return R
     */
    public static ApiResult fail(int code, String msg) {
        return new ApiResult(code, null, msg);
    }

    /**
     * 返回R
     *
     * @param resultCode 业务代码
     * @return R
     */
    public static ApiResult fail(ApiResultCode resultCode) {
        return new ApiResult(resultCode);
    }

    /**
     * 返回R
     *
     * @param resultCode 业务代码
     * @param msg        消息
     * @return R
     */
    public static ApiResult fail(ApiResultCode resultCode, String msg) {
        return new ApiResult(resultCode, msg);
    }

    /**
     * 返回R
     *
     * @param flag 成功状态
     * @return R
     */
    public static ApiResult status(boolean flag) {
        return flag ? success(PalSystemConstant.DEFAULT_SUCCESS_MESSAGE) : fail(PalSystemConstant.DEFAULT_FAILURE_MESSAGE);
    }


    public static ApiResult result(R r) {
        return r.isSuccess() ? success(r.getMsg()) : fail(r.getMsg());
    }
}
